#!/usr/local/bin/node

var path = require("path");
//return max su shu but < n
function sieve(n){
	var a = new Int8Array(n+1);
	var max = Math.floor(Math.sqrt(n));
	var p = 2;
	while(p <= max){
		for(var i=2*p;i<=n;i+=p)
			a[i] = 1;
		while(a[++p]); /* empty */
	}
	while(a[n]) n--;
	return n;
}

//0:node , 1:sieve_node.js , 2:n
if(process.argv.length < 3){
	console.log("must input n value like : " + path.basename(process.argv[1]) + " 1000");
	return;
}

var i = parseInt(process.argv[2]);
if(isNaN(i))
	console.log("must input a number");
else if(i < 0)
	console.log("must input + number");
else{
	var start = new Date().getTime();
	var max_p = sieve(i);
	var end = new Date().getTime() - start;
	console.log("max p is " + max_p + "(take " + end + " ms)");
}